using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using WozLib.UI.Ink;
using WozLib.WozObjects;

namespace WozLib.WozCommands
{
  public class SketchPadWozCommand : WozCommand
  {
    [XmlAttribute("ScreenServerID")]
    public long ScreenServerID
    {
      get
      {
        return _ScreenServerID;
      }
      set
      {
        _ScreenServerID = value;
      }
    } long _ScreenServerID;

    [XmlAttribute("WozServerCommand")]
    public WozCommand WozServerCommand
    {
      get
      {
        return _WozServerCommand;
      }
      set
      {
        _WozServerCommand = value;
      }
    } WozCommand _WozServerCommand;


    public SketchPadWozCommand()
    {
    }

    public SketchPadWozCommand(long screenServerID, WozCommand wozServerCommand, bool pushOntoStack)
    {
      ScreenServerID = screenServerID;
      WozServerCommand = wozServerCommand;

      // get a reference to the WozObjectContainer
      IServerObject iServerObject = ServerObjectRegistry.GetObject(ScreenServerID);

      // check that the IServerObject is in fact a WozObjectContainer
      if (pushOntoStack && iServerObject is SketchPad)
      {
        SketchPad woc = iServerObject as SketchPad;
        if (WozServerCommand != null && woc != null && woc.SinceCommitServerCommandStack != null)
        {

          // push the server command onto the since commit server command stack
          woc.SinceCommitServerCommandStack.Push(wozServerCommand);
        }
      }
    }

    public override void Undo()
    {
      // get a reference to the WozObjectContainer
      IServerObject iServerObject = ServerObjectRegistry.GetObject(ScreenServerID);

      // check that the IServerObject is in fact a WozObjectContainer
      if (iServerObject is SketchPad)
      {
        SketchPad woc = iServerObject as SketchPad;
        if (WozServerCommand != null && woc != null && woc.SinceCommitServerCommandStack != null)
        {
          // Broke
          //if (WozServerCommand.WozServerCommandReferences == null)
          //{
          //  WozServerCommand.WozServerCommandReferences = WozServerCommandReferences;
          //}

          // undo the underlying WozServerCommand
          WozServerCommand.Undo();

          // pop the server command from the since commit server command stack
          if (woc.SinceCommitServerCommandStack.Count > 0 && woc.SinceCommitServerCommandStack.Peek() == WozServerCommand)
          {
            woc.SinceCommitServerCommandStack.Pop();
          }
        }
      }
    }

    public override void Redo()
    {
      // get a reference to the WozObjectContainer
      IServerObject iServerObject = ServerObjectRegistry.GetObject(ScreenServerID);

      // check that the IServerObject is in fact a WozObjectContainer
      if (iServerObject is SketchPad)
      {
        SketchPad woc = iServerObject as SketchPad;
        if (WozServerCommand != null && woc != null && woc.SinceCommitServerCommandStack != null)
        {
          // Broke
          //if (WozServerCommand.WozServerCommandReferences == null)
          //{
          //  WozServerCommand.WozServerCommandReferences = WozServerCommandReferences;
          //}

          // redo the underlying WozServerCommand
          WozServerCommand.Redo();

          // Push the server command onto the since commit server command stack
          woc.SinceCommitServerCommandStack.Push(WozServerCommand);
        }
      }
    }
  }
}
